﻿using Appliaction.Dto.Pinv;
using Appliaction.IAppServices.Pinv;
using AutoMapper;
using Azure.Core;
using Domain.Entitys.Pinv;
using EFCore.Repositorys.Pinv;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers.Pinv
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PbController : ControllerBase
    {
        private readonly IPbService pbService;

        public PbController(IPbService pbService)
        {
            this.pbService = pbService;
        }
        [HttpGet]
        public JsonResult GetPbCount(string? Pb_code, string? Vendor_name)
        {
            return new JsonResult(pbService.GetPbCount(Pb_code, Vendor_name));
        }
        [HttpGet]
        public JsonResult GetPbFyWhere(string? Pb_code, string? Vendor_name, int currentPage, int pageSize)
        {
            return new JsonResult(pbService.GetPbFyWhere(Pb_code, Vendor_name, currentPage, pageSize));
        }
        [HttpPut]
        public async Task<JsonResult> PutPbDelete(PbDeleteDto pinv_Pb)
        {
            return new JsonResult(await pbService.PutPbDelete(pinv_Pb));
        }
        [HttpGet]
        public async Task<JsonResult>  Getpurwhsedetailwherecode(string code)
        {
            var data = await pbService.Getpurwhsedetailwherecode(code);
            return new JsonResult(data);
        }
        [HttpPost]
        public async Task<JsonResult> PostPbAdd([FromBody] PinvOrderPbDetailsDto<Pinv_Pb_AddDto> pinvOrderPbDetailsDto)
        {
            return new JsonResult(await pbService.PostPbAdd(pinvOrderPbDetailsDto));
        }

        [HttpPut]
        public async Task<IActionResult> PutPodetails_Pb_PbdetailsUpdate([FromBody] PBAuditDto pBAuditDto)
        {

            int affectedRows = await pbService.PutPodetails_Pb_PbdetailsUpdate(
            pBAuditDto.Pinv_Po,
            pBAuditDto.Pinv_Details,
            pBAuditDto.Pinv_Pb,
            pBAuditDto.Pinv_Purwhsedetail
        );

            return Ok(new { Success = true, AffectedRows = affectedRows });
        }

        [HttpPut]
        public async Task<JsonResult> PutPoUpdateAudit(Pb_Audit_Dtos pinv_pb)
        {
            return new JsonResult(await pbService.PutPoUpdateAudit(pinv_pb));
        }

        [HttpPut]
        public async Task<JsonResult> PutPbUpdate([FromBody] Pb_details_Update_Dto<PbUpdateDto> pbUpdateDtos)
        {
            return new JsonResult(await pbService.PutPbUpdate(pbUpdateDtos));
        }

        [HttpGet]
        public JsonResult GetPbFyWhereinfo(string? Pb_code, string? Vendor_name, int currentPage, int pageSize)
        {
            return new JsonResult(pbService.GetPbFyWhereinfo(Pb_code, Vendor_name, currentPage, pageSize));
        }
        [HttpGet]
        public JsonResult GetPbCountinfo(string? Pb_code, string? Vendor_name)
        {
            return new JsonResult(pbService.GetPbCountinfo(Pb_code, Vendor_name));
        }
    }
}
